//
//  zijinzhanghuVC.m
//  YaoHeAPP
//
//  Created by xyh on 16/4/6.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "zijinzhanghuVC.h"

#import "Masonry.h"

#import "Controller.h"

#import "TongYong.h"
#import "zhanghumingxiVC.h"

#import "UMMobClick/MobClick.h"

#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height
#define bgVCcolor [UIColor colorWithRed:235.f/255.f green:235.f/255.f blue:244.f/255.f alpha:1]


@interface zijinzhanghuVC ()<UITextFieldDelegate>
{
    NSString *mima1;
    NSString *mima2;

}
@end

@implementation zijinzhanghuVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"zijinzhanghuVC"];
    
   [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"zijinzhanghuVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(60);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"开通资金账户";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.top.offset(31);
    }];

    
    //第一个
    
    for (int i=0; i<2; i++) {
        
    UIView *fcelliew=[[UIView alloc]init];
    fcelliew.backgroundColor=[UIColor whiteColor];
    fcelliew.frame=CGRectMake(0,70+(50+5)*i, screenWidth, 50);
    [self.view addSubview:fcelliew];
    
    UILabel *leftlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 100, 20)];
    leftlabel.font=[UIFont systemFontOfSize:14];
    leftlabel.tag=10+i;
    leftlabel.textAlignment=NSTextAlignmentLeft;
    leftlabel.textColor=[UIColor blackColor];
    [fcelliew addSubview:leftlabel];

    
    UITextField *password=[[UITextField alloc]init];
    password.frame=CGRectMake(100, 10, screenWidth-100-50, 30);
    password.borderStyle=UITextBorderStyleNone;
    password.tag=100+i;
    password.clearButtonMode=UITextFieldViewModeWhileEditing;
    password.secureTextEntry=YES;
    password.font=[UIFont systemFontOfSize:14];
    password.delegate=self;
    [password addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    [password addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [fcelliew addSubview:password];

        if (i==0) {
            leftlabel.text=@"支付密码";
            password.placeholder=@"请输入支付密码";
        }else{
            leftlabel.text=@"确认密码";
            password.placeholder=@"请再次输入支付密码";
        }
        
        
        
        
    }
    
    UIImageView *tishi=[[UIImageView alloc]initWithFrame:CGRectMake(10, 182, 15, 15)];
    tishi.image=[UIImage imageNamed:@"tishi"];
    [self.view addSubview:tishi];
    
    UILabel *tishilabel=[[UILabel alloc]initWithFrame:CGRectMake(28, 180, screenWidth-36, 20)];
    tishilabel.font=[UIFont systemFontOfSize:11];
    tishilabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
    tishilabel.text=@"为保证资金账户安全，请设置支付密码";
    tishilabel.textAlignment=NSTextAlignmentLeft;
    [self.view addSubview:tishilabel];

    
    
    UIButton *surebut=[[UIButton alloc]initWithFrame:CGRectMake((screenWidth-248)/2,250, 248, 40)];
    surebut.backgroundColor=[UIColor colorWithRed:248.f/255.f green:89.f/255.f blue:60.f/255.f alpha:1];
    [surebut setTitle:@"确认" forState:UIControlStateNormal];
    surebut.titleLabel.font=[UIFont systemFontOfSize:16];
    [surebut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [surebut addTarget:self action:@selector(surebutAction) forControlEvents:UIControlEventTouchUpInside];
    surebut.layer.masksToBounds=YES;
    surebut.layer.cornerRadius=20;
    [self.view addSubview:surebut];

    mima1=@"";
    mima2=@"";
    
}

//确定按钮
-(void)surebutAction{
    
    //先判断两个是否为空
    if ([TongYong isBlankObjectAndStr:mima1]) {
        ALERTSHOW(@"密码不能为空");
    }else{
    
       if ([TongYong isBlankObjectAndStr:mima2]) {
        ALERTSHOW(@"请确认密码");
       }else{
       
           if (![mima1 isEqualToString:mima2]) {
               
               ALERTSHOW(@"您两次输入的密码不同，请核对！");
           }else{
           
           //证明可以提交
           
               NSMutableDictionary *dicd=[[NSMutableDictionary alloc]init];
               
               NSString *mimaString=[self hexStringFromString:mima1];
               
               [dicd setValue:mimaString forKey:@"pay_password"];
               
               [Controller requestWithURL:REGISTFINANCE jsondic:dicd finishBlock:^(ClassData* data) {
                   
                   if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
                   {
                       ALERTSHOW(@"成功开通资金账户");
                    
                       zhanghumingxiVC*zijinVC=[[zhanghumingxiVC alloc]init];
                       [self.navigationController pushViewController:zijinVC animated:YES];

                       
                   }else
                   {
                       ALERTSHOW(data.message);
                       
                       
                   }
               }];
               
           }

       
       }
    }
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //    [UIView animateWithDuration:0.25 animations:^{
    //        self.view.frame=CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height);
    //    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSInteger tagnum=textField.tag;
    if (tagnum==100) {
       mima1=textField.text;
    }else if(tagnum==101){
       mima2=textField.text;
    }
 
    //    [UIView animateWithDuration:0.25 animations:^{
    //        self.view.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    //    }];
}
-(void)textFieldDidChanged:(UITextField *)textField
{
    NSInteger tagnum=textField.tag;
    if (tagnum==100) {
        mima1=textField.text;
    }else if(tagnum==101){
        mima2=textField.text;
    }
    
    
}

//普通字符串转换为十六进制的。
-(NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
        
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        
        else
            
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}



//键盘弹下
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)disMiss
{
    return;
}


-(void)backLogin
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
